package fr.polichouz.geotracer.corelocator;

import java.util.Map;

import fr.polichouz.geotracer.shared.AlreadyTakeException;
import fr.polichouz.geotracer.shared.GeotracerService;
import fr.polichouz.geotracer.shared.SimpleLocation;

/**
 * Implementation of the geotracer service.
 * 
 * @author Yoyo
 */
public class GeotracerServiceServerImpl implements GeotracerService {
    
    /** Single instance. */
    private static GeotracerServiceServerImpl INSTANCE;
    
    /**
     * Get the single class instance.
     * @return
     */
    public static synchronized GeotracerServiceServerImpl get() {
	
	if (INSTANCE == null) {
	    INSTANCE = new GeotracerServiceServerImpl();
	}
	return INSTANCE;
    }
    
    /** Accounts (mapping username --> password) */
    private Map<String, String> accounts;
    
    /** Locations (mapping username --> location) */
    private Map<String, SimpleLocation> locations;
    
    /** Persistence layer */
    private PersistenceLayer persistence;
    
    /**
     * Private constructor.
     */
    private GeotracerServiceServerImpl() {
	
	persistence = new FakePersistence();
	
	accounts = persistence.getAccounts();
	locations = persistence.getlocations();
    }

    

    /**
     * @see fr.polichouz.geotracer.corelocator.GeotracerService
     * #createAccount(java.lang.String, java.lang.String)
     */
    @Override
    public void createAccount(String username, String password)
	    throws AlreadyTakeException {
	
	if (username == null || username.length() == 0) {
	    throw new IllegalArgumentException("username is invalid");
	}
	
	if (password == null || password.length() == 0) {
	    throw new IllegalArgumentException("password is invalid");
	}
	
	if (accounts.containsKey(username)) {
	    throw new AlreadyTakeException();
	}
	
	accounts.put(username, password);
	persist();
	
	System.out.println("new account");
    }

    /**
     * @see fr.polichouz.geotracer.corelocator.GeotracerService
     * #updateLocation(fr.polichouz.geotracer.shared.SimpleLocation, 
     * java.lang.String, java.lang.String)
     */
    @Override
    public void updateLocation(SimpleLocation location, String username,
	    String password) {
	
	if (location == null) {
	    throw new IllegalArgumentException("location is null");
	}
	
	if (username == null || username.length() == 0) {
	    throw new IllegalArgumentException("username is invalid");
	}
	
	if (password == null || password.length() == 0) {
	    throw new IllegalArgumentException("password is invalid");
	}
	
	if (accounts.containsKey(username) && 
		accounts.get(username).equals(password)) {
	    
	    locations.put(username, location);
	    persist();
	    
	    System.out.println("new location");
	}
    }

    /**
     * @see fr.polichouz.geotracer.corelocator.GeotracerService
     * #getLocation(java.lang.String, java.lang.String)
     */
    @Override
    public SimpleLocation getLocation(String username, String password) {
	
	
	if (username == null || username.length() == 0) {
	    throw new IllegalArgumentException("username is invalid");
	}
	
	if (password == null || password.length() == 0) {
	    throw new IllegalArgumentException("password is invalid");
	}
	
	if (accounts.containsKey(username) && 
		accounts.get(username).equals(password)) {
	    
	    return locations.get(username);
	} else {
	    return null;
	}
    }
    
    /**
     * Sata datas to persistence layer.
     */
    private void persist() {
	persistence.persist();
    }
}
